var orm = require('orm');
var studentModel = require('./student');
// var settings = require('../../config/settings');
const url = "mysql://localhost:3306/leetest?user=root&password=root&useUnicode=true&characterEncoding=UTF-8";

var connection = null;

function setup(db, cb) {
  //初始化model
  console.log("setup method is invokded");
  studentModel(orm,db);
  return cb(null,db);
}

module.exports = function (cb) {
  if (connection) return cb(null, connection);

  orm.connect(url, function (err, db) {
    if (err) {
      console.error("链接数据库失败，愿意："+err.stack);
      return cb(err,null)
    };
    console.log("数据库连接成功");
    connection = db;
    db.settings.set('instance.returnAllErrors', true);
    setup(db, cb);
  });
};